
$(function () {

    var total = 0; //总页数
    var pageCount = 0;  //分页总数量
    getAlbumList();
    getGradeList();
})

/*获取年级下拉数据*/
function getGradeList() {
    $.DemoPost("/grade/getGradePageByUserInfo?token="+sessionStorage.getItem("token"),
        {"pageNum":1,"pageSize":100},
        function (data) {
            if(data.success)
            {
                $.each(data.obj.list,function (i,d) {
                    $("#gradeList").append(" <option value='"+d.id+"'>"+d.gradeName+"</option>");
                    $("#gradeSelect").append(" <option value='"+d.id+"'>"+d.gradeName+"</option>");
                });
            }
            else{
                layer.msg(data.msg);
            }
        },function (e) {
            layer.msg("初始化数据失败");
        });
}

function gradeChang() {
        var gradeId = $("#gradeList").val();
        if(gradeId=='')
        {
            $("#classList").html( "<option value=''>请选择</option>");
            return;
        }
        $.DemoGet("/classInfo/getClassListByGradeId/"+gradeId,function (data) {
            if(data.success)
            {
                $("#classList").html( "<option value=''>请选择</option>");
                $.each(data.obj,function (i,d) {
                    $("#classList").append(" <option value='"+d.id+"'>"+d.className+"</option>");
                });
            }
            else
            {
                layer.msg(data.msg);
            }
        },function (e) {
            layer.msg("操作失败");
        });
}

function gradeChang1() {
    var gradeId = $("#gradeSelect").val();
    if(gradeId=='')
    {
        $("#classSelect").html( "<option value=''>请选择</option>");
        return;
    }
    $.DemoGet("/classInfo/getClassListByGradeId/"+gradeId,function (data) {
        if(data.success)
        {
            $("#classSelect").html( "<option value=''>请选择</option>");
            $.each(data.obj,function (i,d) {
                $("#classSelect").append(" <option value='"+d.id+"'>"+d.className+"</option>");
            });
        }
        else
        {
            layer.msg(data.msg);
        }
    },function (e) {
        layer.msg("操作失败");
    });
}


function getAlbumList() {
    var gradeId = $("#gradeList").val();
    var classId = $("#classList").val();
    $.DemoPost("/album/getAlbumListByUserInfo?token="+sessionStorage.getItem("token"),
        {"pageNum":1,"pageSize":15,"gradeId":gradeId,"classId":classId},
        function (data) {
            if(data.success)
            {
                if(data.obj.total<1)
                {
                    layer.msg("未获取到相册数据");
                }
                total = data.obj.total;
                pageCount = data.obj.pages;
                $('.M-box').pagination({
                    pageCount: pageCount,//总页面
                    jump: true,
                    coping: true,
                    homePage: '首页',
                    endPage: '末页',
                    mode: 'fixed',
                    prevContent: '上页',
                    nextContent: '下页',
                    callback: PageCallback
                });
                var albumListHtml="";
                var albummanagerListHtml="";
                for(var i=0;i<data.obj.list.length;i++)
                {
                    albumListHtml+='  <div class="big"><a href="'+data.obj.list[i]["albumUrl"]+'" ><img src="'+data.obj.list[i]["albumUrl"]+'" alt="第2张图片描述" title="第2张图片描述"> <span class="c-p-info">2018-08-02</span></a></div>';
                    albummanagerListHtml+='<div class="big" id="'+data.obj.list[i]["id"]+'"><a href="javascript:;"><img src="'+data.obj.list[i]["albumUrl"]+'" ></a></div>';
                }
                $("#dowebok").html(albumListHtml);
                $("#dowebok1").html(albummanagerListHtml);
                $("#manger a").click(function () {
                    $(this).toggleClass("sel");
                })
            }
            else {
                layer.msg(data.msg);
            }
        },function (e) {
           layer.msg("操作失败")
        });
}

  function PageCallback(pageIndex, jq) {
      var gradeId = $("#gradeList").val();
      var classId = $("#classList").val();
      $.DemoPost("/album/getAlbumListByUserInfo?token="+sessionStorage.getItem("token"),
          {"pageNum":pageIndex.getCurrent(),"pageSize":15,"gradeId":gradeId,"classId":classId},
          function (data) {
              if(data.success)
              {
                  var albumListHtml="";
                  var albummanagerListHtml = "";
                  for(var i=0;i<data.obj.list.length;i++)
                  {
                      albumListHtml+='  <div class="big"><a href="'+data.obj.list[i]["albumUrl"]+'" ><img src="'+data.obj.list[i]["albumUrl"]+'" alt="" > </a></div>'
                      albummanagerListHtml+='<div class="big" id="'+data.obj.list[i]["id"]+'"><a href="javascript:;"><img src="'+data.obj.list[i]["albumUrl"]+'" ></a></div>';
                  }
                  $("#dowebok").html(albumListHtml);
                  $("#dowebok-manger").html(albummanagerListHtml);
                  $("#manger a").click(function () {
                      $(this).toggleClass("sel");
                  })
              }
          },function (e) {
              layer.msg("操作失败")
          });
}


$('#dowebok a').simpleLightbox();
//选中
$("#manger a").click(function () {
    $(this).toggleClass("sel");
})
function add(){
    popdiv("grade-info","添加",["50%","50%"],function (index,layero) {
        var arr=new Array();
        $("#yulan .c-p-thumbnail img").each(function(){
            arr.push( $(this).attr("src"));
        })
        var classId = $("#classSelect").val();
        if(classId=='')
        {
            layer.msg("请选择班级");
            return;
        }
        $.DemoPost("/album/addList",
            {"albumList":arr,"classId":classId},function (data) {
            if(data.success)
            {
                layer.close(index);
                getAlbumList();
            }
            else {
                layer.msg(data.msg);
            }
        },function (e) {
                layer.msg('上传失败');
        })


    })
}
function manger(){
    layer.open({
        type: 1 //Page层类型
        ,area: ["50%","80%"]
        ,title: '批量管理'
        ,shade: 0.6 //遮罩透明度
        ,content:$("#manger")
        ,zIndex:999
     //   ,btn:["删除","取消","移动"]
        ,btn:["删除","取消"]
        ,yes:function (index, layero) {
            tan("是否确定删除？",function () {
                var albumList = new Array();
                $("#manger a.sel").parent('.big').each(function () {
                    albumList.push(this.id);
                });
                $.DemoPost("/album/delete",{"albumList":albumList},
                    function (data) {
                        if(data.success)
                        {
                            layer.close(index);
                            getAlbumList();
                        }
                        else {
                            layer.msg(data.msg);
                        }

                    },function (e) {
                        layer.msg("删除数据失败");
                    });
            });
        }
        // ,
        // btn3:function(index, layero) {
        //     layer.close(index);
        //     popdiv("selPicture","选择",["50%","50%"],function (index,layero) {
        //         layer.close(index);
        //     })
        //     return false;
        // }
    });

}

function upImg(obj) {
    var formData = new FormData();
    formData.append('file',$('#file')[0].files[0]);
    $.ajax({
        url: 'http://139.129.213.125:6060/sports/api/picture',
        type: 'POST',
        cache: false,
        data: formData,
        processData: false,
        contentType: false
    }).done(function(res) {
        console.log(res);
        var html ='<div class="col-md-2 c-p-thumbnail pull-left c-p-w-h">' +
            '                    <img src="'+res[0]+'" alt="">' +
            '                    </div>';
        $("#yulan").prepend(html);
    }).fail(function(res) {});
}

$("#btnQuery").click(function () {
    getAlbumList();
});